<template>
	<block>
		<van-notify class="hxt-notify" />
		<!--加载中-->
		<block v-if="!appInfo">
			<hxt-loading :show="true"></hxt-loading>
		</block>
		<!--异常-->
		<block v-else-if="appInfo.code!=0">
			<hxt-pagetip title="出错了" :msg="appInfo.errmsg" :okbtn="tryTimes>0?'重新加载（'+tryTimes+'）':''" @onOk="onRetry"
				exit="true"></hxt-pagetip>
		</block>
		<block v-else>
			<view class="hxt-flex hxt-flex-column" style="height:100%;width:100%;overflow:hidden;">
				<view class="hxt-flex-fill" style="overflow: hidden;">
					<!--首页-->
					<block v-if="tabIndex==0">
						<hxt-main :menuList="menuList"></hxt-main>
					</block>
					<!--我的-->
					<block v-else-if="tabIndex==1">
						<view class="hxt-flex hxt-flex-column">
						</view>
					</block>
				</view>
				<!--底部选项卡-->
				<!-- <view class="cu-list grid col-2 no-border"
					style="padding:0; line-height:24rpx; border-top:1rpx solid #d7d7d8; background-color: #F9F9F9;">
					<view v-for="(item,index) in tabList" :key="item.name" class="cu-item" style="padding: 0;"
						@click="switchTab(index)">
						<view :class="['cuIcon-'+item.icon, index==tabIndex?'hxt-color':'text-grey']"
							style="font-size: 50rpx;">
						</view>
						<text :class="[index==tabIndex?'hxt-color':'text-grey']">{{item.name}}</text>
					</view>
				</view> -->
			</view>

		</block>
		<van-dialog id="van-dialog" />
		<!--加载层-->
		<hxt-loading id="hxt-loading"></hxt-loading>
	</block>
</template>

<script>
	import Dialog from '@/wxcomponents/vant/dialog/dialog';
	import hxt from "@/utils/hxt.js"
	import v3 from "@/utils/wxPayV3.js"
	const app = getApp()
	export default {
		data() {
			return {
				tabList: [{
						icon: 'cascades',
						name: '首页'
					},
					{
						icon: 'people',
						name: '我的'
					}
				],
				tabIndex: 0,
				appInfo: app.globalData.appInfo,
				menuList: [],

			}
		},
		onLoad(query) {
			var _this = this;
			if (query.q && query.scancode_time) {
				hxt.log('query.q', query.scene);
				var q = ('' + decodeURIComponent(query.q)).split('?');
				if (q.length > 1) {
					var parms = q[1].split('&');
					for (var i = 0; i < parms.length; i++) {
						var parm = parms[i].split('=');
						if (parm.length == 2) {
							query[parm[0]] = parm[1];
						}
					}
				}
				query.q = '';
				query.scancode_time = '';
			}
			if (!!query.scene) {
				hxt.log('query.scene', query.scene);
				var str = hxt.base64Decode('' + query.scene).split('/');
				if (str.length > 1) {
					if (str[0] == '0') {
						var gdId = Number(str[1]);
						if (!isNaN(gdId)) {
							query.state = 'suborder';
							query.gdId = gdId;
							query.scene = null;
						}
					}
				}
			}
			// query.state = 'schedule';
			//query.scan = 'a4dc947e3f804708a2a4051bd01ef579';
			// query.user = "o4HZh4-ElYaBYFZ8XqprgIEjK6XA";
			//query.id = '12343';
			_this.initData(query);
			// _this.$nextTick(function(){
			// 	_this.initData(query, 0);
			// });
		},
		onShow: function() {
			this.showPage();
		},
		onShareAppMessage: function() {

		},
		methods: {
			showPage: function() {
				var _this = this;
				_this.setData({
					appInfo: app.globalData.appInfo
				});
				// uni.$on('onRegSwitchUser', function(data) {
				// 	if (data.uiId > 0) {
				// 		_this.switchUser(data.uiId);
				// 	}
				// 	//console.log('返回带回的数据：');
				// 	//console.log(data);
				// });
			},
			setMenus: function(query) {
				var _this = this;
				console.log('setMenu', _this.appInfo)
				var _this = this,
					currentUser = _this.appInfo.currentUser;
				var createMenu = function(pId, list) {
					var arr = $.grep(currentUser.uiAccessList, function(item, ix) {
						return item.flpId == pId;
					});
					$.each(arr, function(ix, item) {
						var m = {
							id: item.Id,
							pId: item.flpId,
							level: item.level,
							name: item.name,
							state: item.state,
							path: '/' + item.flId,
							children: []
						};
						createMenu(m.id, m.children);
						list.push(m);
					});
				};
				createMenu(1, menuList);
				console.log(menuList, 'menuList');
			},
			initData: function(query, target) {
				var _this = this,
					menuList = _this.menuList;
				hxt.getAppInfo(query, function(res) {
					hxt.loading(false);
					_this.showPage();
					//成功
					if (res.code == 0) {
						//初始加载
						var appInfo = _this.appInfo;
						var obj = app.globalData.currentUser.uiAccessList.find(x => {
							return x.state == query.state;
						});
						
						if (obj) {
							wx.navigateTo({
								url: obj.url
							});
						};
						//清除state,id
						var storage = wx.getStorageSync('appInfo');
						if (!!storage) {
							storage.state = '';
							storage.id = 0;
							wx.setStorageSync('appInfo', storage);
							app.globalData.appInfo = storage;
							var createMenu = function(pId, list) {
								var arr = app.globalData.currentUser.uiAccessList.filter(function(item, ix) {
									return item.flpId == pId && item.show != 1 && item.show != 99;
								});
								arr.forEach(function(item) {
									var m = {
										id: item.flId,
										pId: item.flpId,
										level: item.level,
										name: item.flName,
										state: item.state,
										icon: item.icon,
										url: item.url,
										show: item.show,
										color: item.color,
										children: []
									};
									createMenu(m.id, m.children);
									list.push(m);
								});
							};
							createMenu(1, menuList);
							_this.menuList = menuList.sort(function(a, b) {
								return a.id - b.id;
							});
							console.log('_this.menuList', _this.menuList);
						};
					}
				});

			},
			// 菜单点击
			clickMenu: function(menu) {
				var _this = this;
				wx.navigateTo({
					url: menu.url,
					complete: function(res) {
						console.log(res)
					}
				});
			},

			//切换选项卡

			switchTab: function(idx) {
				this.setData({
					inputAccount: null,
					accountFocus: false
				});
				if (idx != this.tabIndex) {
					this.setData({
						tabIndex: idx
					});
				}
			},
		}
	}
</script>

<style>
	.hxt-index-menuimg {
		height: 60rpx;
		width: 60rpx;
		line-height: 60rpx;
	}

	.hxt-index-menuimg text {
		font-size: 42rpx !important;
		margin-top: 0 !important;
	}

	.cu-load.load-modal {
		box-shadow: 0 0 0rpx 2000rpx rgba(0, 0, 0, 0.2);
	}

	.hxt-color {
		color: #00b1ff !important;
	}

	.loginput {
		color: #000;
		display: block !important;
		text-align: left;

	}

	.loginput[class*="line"]::after {
		border: 1px solid #1cbbb4;
	}

	.cu-avatar {
		overflow: hidden;
	}

	.cu-avatar image {
		height: 100%;
		width: 100%;
		object-fit: cover;
	}

	.wxbtn {
		border: none;
		background-color: transparent;
		line-height: inherit;
	}

	.wxbtn:after {
		border: none;
		height: auto;
	}
</style>